<!-- 修改系统连接页面 -->
<template>
	<view style="width: 100vw; height: 100vh;background-color: #fff;">
		<newNavBar></newNavBar>
		<uni-forms ref="baseForm" :modelValue="baseFormData" class="content" label-position="top">
			<uni-forms-item label="IP地址" labelWidth="60px" name="ip">
				<uni-easyinput class="uni-forms-item-border" v-model="baseFormData.ip" placeholder="请输入IP地址">
				</uni-easyinput>
			</uni-forms-item>
			<uni-forms-item label="端口号" labelWidth="60px" name="port">
				<uni-easyinput class="uni-forms-item-border" v-model="baseFormData.port" placeholder="请输入端口号">
				</uni-easyinput>
			</uni-forms-item>
			<uni-forms-item>
				<button :style="{borderRadius: '40px', backgroundColor:mainColor,color: '#fff'}" type="primary"
					@click="submit">保存设置</button>
			</uni-forms-item>

		</uni-forms>
	</view>
</template>

<script>
	import * as SystemJs from '../../common/system.js'
	import * as configJs from '../../common/config.js'
	export default {
		data() {
			return {
				mainColor: '',
				baseFormData: {
					ip: '192.168.1.7',
					port: '8022'
				}
			}
		},
		onShow() {
			this.mainColor = configJs.baseColor.mainTitleColor;
			var systemConfig = uni.getStorageSync("systemConfig")
			if (!SystemJs.systemJs.IsEmpty(systemConfig)) {
				this.baseFormData = JSON.parse(systemConfig)
			}
		},
		onLoad() {
			SystemJs.systemJs.showTitleBar('连接设置', true)
		},
		methods: {
			submit() {
				if (SystemJs.systemJs.IsEmpty(this.baseFormData.ip)) {
					uni.showToast({
						title: "请输入IP地址",
						icon: 'error'
					})
					return;
				}
				if (SystemJs.systemJs.IsEmpty(this.baseFormData.port)) {
					uni.showToast({
						title: "请输入端口号",
						icon: 'error'
					})
					return;
				}

				// #ifdef APP-PLUS
				var url = 'http://' + this.baseFormData.ip + ':' + this.baseFormData.port + '/App/UpdateConnect'
				// #endif
				// #ifdef H5
				var url = '/api/App/UpdateConnect'
				// #endif
				console.log(url)
				uni.showLoading({
					title: '加载中',
					mask: true
				})
				var obj = {
					serviceIP: this.baseFormData.ip
				}
				//
				var Postdata = SystemJs.cryptoJS.encrypt(JSON.stringify(obj))
				console.log(Postdata)
				uni.request({
					// url: url + '/App/UpdateConnect',
					url: url,
					data: Postdata,
					method: 'POST',
					header: {
						'content-type': 'application/json;charset:utf-8'
					},
					success: (res) => {
						uni.hideLoading();
						if (res.statusCode == 200) {
							let data = JSON.parse(SystemJs.cryptoJS.decrypt(res.data.toString()))
							if (data.Result) {
								uni.setStorageSync('systemConfig', JSON.stringify(this
									.baseFormData));
								//将目标ip端口号地址存入vit变量中
								// process.env.targetUrl = SystemJs.httpUnit.getBaseUrl('');
								// var timeId = setTimeout(() => {

								// 	clearTimeout(timeId)
								// }, 500)
								SystemJs.httpUnit.uniToast(JSON.stringify(data.Message))
							} else {
								SystemJs.httpUnit.uniToast("连接失败")
							}
						} else {
							SystemJs.httpUnit.uniToast(JSON.stringify(res))
						}
					},
					fail: (err) => {
						uni.hideLoading();
						SystemJs.httpUnit.uniToast(JSON.stringify(err))
					}
				})
				// uni.showLoading({
				// 	title:'正在连接...'
				// })

			}

		}
	}
</script>

<style>
	.content {
		padding: 15px;
	}

	.uni-forms-item {
		margin-bottom: 0px;
	}

	/* 穿透修改输入框样式 */
	/deep/.is-input-border {
		border: none;
		/* border-bottom: 1px solid #f0f0f0; */
	}

	.uni-forms-item-border {
		border: none;
		border-bottom: 1px solid #e6e6e6;
		border-radius: 4px;
	}
</style>